package com.liangxy.zhyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.liangxy.zhyl.base.PageResponse;
import com.liangxy.zhyl.dto.UserDto;
import com.liangxy.zhyl.entity.User;
import com.liangxy.zhyl.exception.BaseException;
import com.liangxy.zhyl.mapper.UserMapper;
import com.liangxy.zhyl.service.UserService;
import com.liangxy.zhyl.utils.UserThreadLocal;
import com.liangxy.zhyl.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.liangxy.zhyl.constant.DefaultPasswordConstant.DEFAULT_PASSWORD;
import static com.liangxy.zhyl.enums.BasicEnum.USER_IS_ABLE;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResponse<UserVo> page(int pageNum, int pageSize, UserDto userDto) {
        PageHelper.startPage(pageNum, pageSize);
        Page<List<UserVo>> page = userMapper.selectPage(userDto);
        PageResponse<UserVo> pageResponse = PageResponse.of(page, UserVo.class);
        return pageResponse;
    }

    @Override
    public Boolean update(UserDto userDto) {
        User user = new User();
        BeanUtil.copyProperties(userDto, user);
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public UserVo getCurrentUser() {
        Long id = UserThreadLocal.getUserId();
        User user = userMapper.selectByPrimaryKey(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public Boolean updateIsEnable(Long id, Integer status) {
        User user = new User();
        user.setId(id);
        user.setIsDelete(status);
        return userMapper.updateByPrimaryKeySelective(user) > 0;
    }

    @Override
    public List<UserVo> list(UserDto userDto) {
        List<UserVo> userVoList = userMapper.selectList(userDto);
        return userVoList;
    }

    @Override
    public Boolean remove(Long userIds) {
        if (userIds == null) {
            return false;
        }
        User user = userMapper.selectByPrimaryKey(userIds);
         user.getDataState();
        if ("0".equals(user.getDataState())) {
            // 启用的用户不可被删除
            throw new BaseException(USER_IS_ABLE);
        }
        // user.setIsDelete(1);
        userMapper.deleteByPrimaryKey(userIds);
        return true;
    }

    @Override
    public Boolean resetPasswords(Long userId) {
        User user = new User();
        user.setId(userId);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode(DEFAULT_PASSWORD));
        userMapper.updateByPrimaryKeySelective(user);
        return true;
    }

    @Override
    public Boolean add(UserDto userDto) {
        User user = new User();
        BeanUtil.copyProperties(userDto, user);
        user.setPassword(new BCryptPasswordEncoder().encode(DEFAULT_PASSWORD));
        userMapper.insertSelective(user);
        return user.getId() > 0;
    }
}
